<?php

use App\Cats;
use App\Trance;
use Illuminate\Http\Request;
use App\Http\Controllers\AuthController;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/tts', function (Request $request) {
//    echo 'edge-tts --voice ru-RU-DmitryNeural --text "' . $request->rus . '" --write-media output.mp3';
    // 创建本地存储中的文件夹
    $username = $request->username;
    $path = public_path($username);
    if (!file_exists($path)) {
        mkdir($path, 0755, true);
        //Folder created successfully
    }
    exec('edge-tts --voice ru-RU-DmitryNeural --text "' . $request->rus . '" --write-media ' . $username . '/output.mp3 --write-subtitles hello.srt ', $output);

    return $output;


});

Route::post('/trance', function (Request $request) {
    $q = $request->q;
    $appid = '20241206002221548';
    $secKey = 'PaFufSVv3w8nHyrk3IJT';
//    $salt = random_int(10000000, 99999999);
    $salt = '147896325';
    $sign = md5($appid . $q . $salt . $secKey);
    $response = Http::get('http://api.fanyi.baidu.com/api/trans/vip/translate', [
        'q' => $q,
        'from' => $request->from,
        'to' => $request->to,
        'salt' => $request->salt,
        'appid' => $appid,
        'sign' => $request->sign,
    ]);

    return $response->json();

});

Route::middleware('cors')->group(function () {

    Route::get('/cats', function (Request $request) {
        $activities = Cats::orderBy('sort', 'ASC')->get();
        echo $activities;
    });


    Route::get('/cat/{id}', function (Request $request, string $id) {
        $activities = Trance::where('cat_id', $id)->get();
        echo $activities;
    });
});





Route::get('/search', function (Request $request) {
    $activities = Trance::where('ru', 'like', '%' . $request->q . '%')->get();
    echo $activities;
});


Route::post('/register', [AuthController::class, 'register']);

Route::post('/login', [AuthController::class, 'login'])->name('login');


Route::middleware('auth:sanctum')->group(function () {
//    Route::get('/user', function (Request $request) {
//        return $request->user();
//    });

    Route::post('logout', [AuthController::class, 'logout'])->name('logout');
//    Route::post('refresh-token', [AuthController::class, 'refreshToken']);

});


Route::get('/wx/auth_code', function (Request $request) {

    $response = Http::get('https://api.weixin.qq.com/sns/oauth2/access_token', [
        'appid' => $request->appid,
        'secret' => $request->secret,
        'code' => $request->code,
        'grant_type' => 'authorization_code',
    ]);

    return $response->json();
});



